package com.yunlinker.asba.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yunlinker.asba.model.SaveImg;


public class DatabaseUtil {
	private MyHelper helper;

	public DatabaseUtil(Context context) {
		super();
		helper = new MyHelper(context);
	}
	
	/**��������
	 * @param
	 * */
	public boolean Insert(SaveImg person){
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into "+MyHelper.TABLE_NAME
					+"(url,urldate,urltime,urlsize) values ("
					+ "'"+person.getUrl()+ "' ,"
					+ "'"+person.getUrldate()+ "' ,"
					+ "'"+person.getUrltime()+ "' ,"
					+ "'"+ person.getUrlsize() + "'" + ")";
		try {            
			db.execSQL(sql); 
			return true;
			} catch (SQLException e){  
				Log.e("err", "insert failed"); 
				return false;
				}finally{
					db.close();
				}
		
	}


	/**
	 *
	 * @param person
	 * @param id
     */
	public void Update(SaveImg person ,int id){
		
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("urldate", person.getUrldate());
		values.put("urltime", person.getUrltime());
		values.put("urlsize", person.getUrlsize());
		int rows = db.update(MyHelper.TABLE_NAME, values, "_id=?", new String[] { id + "" });
		
		db.close();
	}


	/**
	 *
	 * @param id 根据id删除表数据
     */
	public void Delete(int id){
		SQLiteDatabase db = helper.getWritableDatabase();
		int raw = db.delete(MyHelper.TABLE_NAME, "_id=?", new String[]{id+""});
		db.close();
	}

	/**
	 *
	 * @return 查询所有的信息
     */
	public List<SaveImg> queryAll(){
		SQLiteDatabase db = helper.getReadableDatabase();
		List<SaveImg> list = new ArrayList<SaveImg>();
		Cursor cursor = db.query(MyHelper.TABLE_NAME, null, null,null, null, null, null);
		
		while(cursor.moveToNext()){
			SaveImg person = new SaveImg();
			 person.setId(cursor.getInt(cursor.getColumnIndex("_id")));   
			 person.setUrl(cursor.getString(cursor.getColumnIndex("url")));
			 person.setUrldate(cursor.getString(cursor.getColumnIndex("urldate")));
			 person.setUrltime(cursor.getLong(cursor.getColumnIndex("urltime")));
			 person.setUrlsize(cursor.getLong(cursor.getColumnIndex("urlsize")));
			 list.add(person);
		}
		db.close();
		return list;
	}


	/**
	 *
	 * @param name 需要查询的url
	 * @return 根据url查询本地是否有数据
     */
	public List<SaveImg> queryByname(String name){
		SQLiteDatabase db = helper.getReadableDatabase();
		List<SaveImg> list = new ArrayList<SaveImg>();
		Cursor cursor = db.query(MyHelper.TABLE_NAME, new String[]{"_id","url","urldate","urltime","urlsize"}, "url like ? " ,new String[]{"%" +name + "%" }, null, null, "url asc");
//		Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
		while(cursor.moveToNext()){
			SaveImg person = new SaveImg();
			person.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			person.setUrl(cursor.getString(cursor.getColumnIndex("url")));
			person.setUrldate(cursor.getString(cursor.getColumnIndex("urldate")));
			person.setUrltime(cursor.getLong(cursor.getColumnIndex("urltime")));
			person.setUrlsize(cursor.getLong(cursor.getColumnIndex("urlsize")));
			 list.add(person);
		}
		db.close();
		return list;
	} 
	
	
//	public SaveImg queryByid(int id){
//
//		SQLiteDatabase db = helper.getReadableDatabase();
//		SaveImg person = new SaveImg();
//		Cursor cursor = db.query(MyHelper.TABLE_NAME, new String[]{"name","sex"}, "_id=?",new String[]{ id + ""}, null, null, null);
////		db.delete(table, whereClause, whereArgs)
//		while(cursor.moveToNext()){
//			 person.setId(id);
//			 person.setName(cursor.getString(cursor.getColumnIndex("name")));
//			 person.setSex(cursor.getString(cursor.getColumnIndex("sex")));
//		}
//		db.close();
//		return person;
//	}
	

}
